<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        div {
            width: 200px;
            margin: 20px auto;
            display: flex;
            /* border: 1px solid black; */
        }

        span {
            display: block;
            width: 60%;
            height: 20px;
            line-height: 20px;
            text-align: center;
            border: 1px solid black;
            margin-right: 10px;
            font-weight: 600;
            color: orangered;
        }

        button {
            display: block;
            height: 20px;
            width: 40%;
            background: orangered;
        }

        input {
            display: block;
            margin: 10px auto;
        }
    </style>
</head>

<body>
    <div>
        <span id="show">放置验证码</span><button id="btn">看不清</button>
    </div>

    <input type="text" id="input">
    <script>
        /* 
        1、操作谁就获取谁
        2、封装了一个随机产生验证码的方法
        3、给btn绑定点击事件，当点击的时候去产生一个随机验证码
        4、给input绑定失去焦点事件，当失去焦点时，判断两个值是否相等
        */
        let show = document.getElementById('show'),
            btn = document.getElementById('btn'),
            input = document.getElementById('input');
        // console.log(show,btn,input);
        function createCode(str, num = 4) {
            // console.log(str);
            str = str || 'qwertyui12345678ASDFGH';
            let value = '';
            // 产生四位不重复的验证码
            for (var i = 0; i < num; i++) {
                let index = Math.round(Math.random() * (str.length - 1 - 0) + 0);
                value += str[index];
            }

            // return value;
            show.innerText = value;
        }


        createCode();
        btn.onclick = function(){
            createCode();
        };

        input.onblur = function(){
            // 1.获取到input的内容  获取到span中的内容
            // 2.把内容全部转小写然后在进行比较
            let inputValue = input.value.toLowerCase();
            let spanValue = show.innerText.toLowerCase();
            if(inputValue === spanValue){
                alert('ok')
            }
            else {
                alert('no');
                createCode();
                input.value = ''
            }
        }
       

    </script>
</body>

</html>